/**
 * @(#)com.tansun.easycare.rule.excel.utils.DomainUtil.java Copyright (c) 2017 天阳宏业科技股份有限公司
 */
package com.tansun.easycare.rule.excel.utils;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tansun.easycare.rule.datamodel.dao.RuleDataDomainDao;
import com.tansun.easycare.rule.datamodel.dao.RuleDataparamsDao;
import com.tansun.easycare.rule.datamodel.dao.RuleDatasetDao;
import com.tansun.easycare.rule.datamodel.entity.RuleDataDomain;

/**
 * domain工具类
 * @author zengdong
 * @version 1.0  2017年5月24日
 * @modified zengdong  2017年5月24日  <创建>
 */
//@Lazy(false)
//@Component
@Service("paramsUtil")
public class ParamsUtil {

    @Autowired
    private RuleDataDomainDao ruleDataDomainDao;

    @Autowired
    private RuleDataparamsDao ruleDataparamsDao;

    @Autowired
    private RuleDatasetDao ruleDatasetDao;

    public static RuleDataDomainDao domainDao;

    public static RuleDataparamsDao paramsDao;

    public static RuleDatasetDao datasetDao;

    @PostConstruct
    public void initUtil() {
        domainDao = ruleDataDomainDao;
        paramsDao = ruleDataparamsDao;
        datasetDao = ruleDatasetDao;
    }

    /**
     * 根据ID获取Domain值
     * @param id
     * @return
     */
    public String getDomainById(String domainId, String value) {
        StringBuffer sb = new StringBuffer();
        if (domainDao != null) {
            RuleDataDomain searchDomain = new RuleDataDomain();
            searchDomain.setDomainId(domainId);
            //容错处理
            if (!value.startsWith("[")) {
                value = "[\"" + value + "\"]";
            }
            List<String> list = JSON.parseArray(value, String.class);
            if (list != null && list.size() > 0) {
                for (String str : list) {
                    searchDomain.setDomainValue(str);
                    List<RuleDataDomain> domainList = domainDao.findList(searchDomain);
                    if (domainList != null && domainList.size() > 0) {
                        for (RuleDataDomain dom : domainList) {
                            sb.append("," + dom.getName());
                        }
                    }
                }
            }
        }
        return StringUtils.isNotBlank(sb.toString()) ? sb.substring(1) : "";
    }
}
